package com.Socket.lianxi.homework;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class SocketTCPClient {
    public static void main(String[] args)throws IOException {
        //建立9999端口链接
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println(socket.getClass());
        //通过输出流，把数据写道数据通道，使用字符串
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        //键盘读取用户输入
        int num = 0;
        while (true){
            if (num < 3){
                Scanner scanner = new Scanner(System.in);
                System.out.println("输入你的问题");
                String question = scanner.next();
                ++num;
                bufferedWriter.write(question);
                bufferedWriter.newLine();//插入一个换行符，表示写入内容已经结束
                bufferedWriter.flush();//手动刷新，如果使用字符流的话

                InputStream inputStream = socket.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String s  = bufferedReader.readLine();
                System.out.println(s);



            }else {
                return;
            }
//            bufferedReader.close();
            bufferedWriter.close();
            outputStream.close();
//            inputStream.close();
            socket.close();
        }




    }
}
